package com.zhiche.lisa.bms.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.BMSApplication;
import com.zhiche.lisa.bms.dao.model.fee.FeeReceivable;
import com.zhiche.lisa.bms.service.utils.UaaUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import java.util.*;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BMSApplication.class)
public class FeeReceivableControllerText {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    private String token;

    @Autowired
    private UaaUtil uaaUtil;

    @Autowired
    private Filter springSecurityFilterChain;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain).build();
//        token = uaaUtil.getTenantToken("15110288389");
        token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnbXRfY3JlYXRlIjoiMTUzNzE3ODU1NTAwNyIsImFjY291bnRJZCI6MTAyLCJ1c2VyX25hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIiwic2NvcGUiOlsiYWxsIl0sInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJ0ZW5hbnRJZCI6MSwiY29ycE5hbWUiOiLkuK3ogZTnianmtYHvvIjkuK3lm73vvInmnInpmZDlhazlj7giLCJleHAiOjE1MzcxODU3NTUsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiOTg3ZDI2ZTQtZGU5Zi00NWQ5LThmZTYtZjdiMjU5ZmUxNDRkIiwiY2xpZW50X2lkIjoiZGV2b3BzIiwidXNlcm5hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIn0.gO9BmKlSjUz0YA0bzHvX2AIbJHAamxHsFZ_rfMBzuq8";
    }


    @Test
    public void lisaFeeReceivableLineText() throws Exception {
        Page<FeeReceivable> page = new Page<>(1, 10);
        Map<String, Object> map = new HashMap<>();
        map.put("loadStatus", 10);
        page.setCondition(map);
        String a = JSONObject.toJSONString(page);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/feeReceivable/lisaFeeReceivableLine")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status,200);
    }
    @Test
    public void selectContractNameText()throws Exception{
        String a = JSONObject.toJSONString(null);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/feeReceivable/selectContractName")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status,200);
    }
    @Test
    public void queeryLspNameByLspId()throws Exception {
        String a = JSONObject.toJSONString(null);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.get("/feeReceivable/queeryLspNameByLspId")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果状态：" + status);
        System.out.println(content);
        Assert.assertEquals(status, 200);
    }
    @Test
    public void queryFeeReceivablebms()throws Exception{
        List<FeeReceivable> listD = new ArrayList<>();
        FeeReceivable lisaFeeReceivable = new FeeReceivable();
        lisaFeeReceivable.setReceiptDate(new Date());
//        lisaFeeReceivable.setTenantId(26L);
//        lisaFeeReceivable.setWaybillDtlId("ULC/ZC.ZC201888888885");
        lisaFeeReceivable.setWaybillDtlId("ULC/ZC.ZC2018080301066");
        lisaFeeReceivable.setWaybillId("W20180911");
        lisaFeeReceivable.setPayeeId(16L);
        lisaFeeReceivable.setPayerId(25l);
//        lisaFeeReceivable.setVehicleTypeName("全顺V348长轴v柴");
        FeeReceivable lisaFeeReceivable2 = new FeeReceivable();
        lisaFeeReceivable2.setReceiptDate(new Date());
//        lisaFeeReceivable2.setTenantId(28L);
//        lisaFeeReceivable2.setWaybillDtlId("ULC/ZC.ZC201888888886");
        lisaFeeReceivable2.setWaybillDtlId("ULC/ZC.ZC2018080301067");
        lisaFeeReceivable2.setWaybillId("W20180912");
        lisaFeeReceivable2.setPayeeId(14L);
        lisaFeeReceivable2.setPayerId(27l);
//        lisaFeeReceivable2.setVehicleTypeName("新全顺物流车短轴v柴");
        listD.add(lisaFeeReceivable);
        listD.add(lisaFeeReceivable2);
        String a = JSONObject.toJSONString(listD);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/feeReceivable/insertFeeReceivablebms")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization", token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果：" + status);
        System.out.println(content);
        Assert.assertEquals(status, 200);

    }
}

